package com.anyonavinfo.cpaddemo.data.musicinfo.source.local;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
<<<<<<< HEAD
import android.util.Log;
=======
>>>>>>> b56716b621890f59100a220d4c7abe0ddb29e232

import com.anyonavinfo.cpaddemo.Application;
import com.anyonavinfo.cpaddemo.data.musicinfo.MusicInfo;
import com.anyonavinfo.cpaddemo.data.musicinfo.source.MusicInfoDataSource;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZuiC on 2017/1/23.
 */

<<<<<<< HEAD
public class MusicInfoLocalDataSource {
=======
public class MusicInfoLocalDataSource implements MusicInfoDataSource {
>>>>>>> b56716b621890f59100a220d4c7abe0ddb29e232


    private static MusicInfoLocalDataSource INSTANCE;

    public static MusicInfoLocalDataSource getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new MusicInfoLocalDataSource();
        }
        return INSTANCE;
    }

    private MusicInfoLocalDataSource(){}

<<<<<<< HEAD

    public void loadMusicInfo(MusicInfoDataSource.LoadMusicInfoCallBack callBack) {
=======
    @Override
    public void loadMusicInfo(LoadMusicInfoCallBack callBack) {
>>>>>>> b56716b621890f59100a220d4c7abe0ddb29e232
        List<MusicInfo> musicInfo = new ArrayList<>();

        String[] projection = {
                MusicInfoPersistenceContract.MusicInfoEntry.COLUMN_NAME_ID,
                MusicInfoPersistenceContract.MusicInfoEntry.COLUMN_NAME_DATA,
                MusicInfoPersistenceContract.MusicInfoEntry.COLUMN_NAME_DISPLAY_NAME,
                MusicInfoPersistenceContract.MusicInfoEntry.COLUMN_NAME_SIZE,
                MusicInfoPersistenceContract.MusicInfoEntry.COLUMN_NAME_TITLE,
                MusicInfoPersistenceContract.MusicInfoEntry.COLUMN_NAME_DURATION,
                MusicInfoPersistenceContract.MusicInfoEntry.COLUMN_NAME_ALBUM_ARTIST,
                MusicInfoPersistenceContract.MusicInfoEntry.COLUMN_NAME_ARTIST,
                MusicInfoPersistenceContract.MusicInfoEntry.COLUMN_NAME_ALBUM,
                MusicInfoPersistenceContract.MusicInfoEntry.COLUMN_NAME_ALBUM_KEY
        };


        ContentResolver cr = Application.getContextObject().getContentResolver();
        Cursor c = cr.query(MusicInfoPersistenceContract.MusicInfoEntry.TABLE_NAME, projection, null, null, null);

       if (c != null) {
            for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
                MusicInfo music = new MusicInfo();
                music.setId(c.getLong(0));//歌曲ID
                music.setPath(c.getString(1));//路径
                music.setDisplayName(c.getString(2));//文件名
                music.setSize(c.getInt(3));//文件大小
                music.setTitle(c.getString(4));//歌曲名
                music.setDuration(c.getInt(5));//时长
                music.setArtist(c.getString(7));//歌唱者
                music.setAlbumKey(c.getString(9));
                music.setAlbumArt(getAlbumArtByAlbumKey(c.getString(9)));
<<<<<<< HEAD
                Log.e("ZUIZUI",music.getTitle() + "  " + music.getPath());
=======
>>>>>>> b56716b621890f59100a220d4c7abe0ddb29e232
                if (new File(music.getPath()).exists()
                        //时间大于60秒的播放
                        && music.getDuration()>1000*60
                        //pad无法解析wma类型文件，对此进行屏蔽
                        && !music.getDisplayName().toLowerCase().endsWith(".wma")
                        ){
                    musicInfo.add(music);
                }
            }
            c.close();
            callBack.onMusicInfoLoaded(musicInfo);
        }
    }


    /**
     * 根据album_key找album_art的值
     * @param albumKey
     * @return album_art，即图片的路径，如果无法读取到内置图片，则返回null
     */
    public String getAlbumArtByAlbumKey(String albumKey) {
        if (albumKey == null) {
            return null;
        }
        ContentResolver cr = Application.getContextObject().getContentResolver();
        Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
        String[] projection = { "album_art" };
        String selection = "album_key=?";
        String[] selectionArgs = { albumKey };
        String sortOrder = null;
        Cursor c = cr.query(uri, projection, selection, selectionArgs, sortOrder);

        if(c != null && c.moveToFirst()) {
            String art=c.getString(c.getColumnIndex("album_art"));
            c.close();
            return art;
        }
        c.close();
        return null;
    }
}
